import { defHttp } from "/@/utils/http/axios"
import { Modal } from "ant-design-vue"
import * as iamCache from '/@/utils/cache/module/iam'
enum Api {
  list = "/infra-service/dict/page",
  save = "/infra-service/dict/save",
  edit = "/infra-service/dict/update",
  getDict = "/infra-service/dict/get",
  duplicateCheck = "/system-service/duplicate/check",
  deleteDict = "/infra-service/dict/delete",
  deleteBatch = "/infra-service/dict/deleteBatch",
  recycleBinList = "/infra-service/dict/deleteList",
  putRecycleBin = "/infra-service/dict/back",
  deleteRecycleBin = "/infra-service/dict/deletePhysic",
  itemList = "/infra-service/dictItem/listItem",
  deleteItem = "/infra-service/dictItem/delete",
  itemSave = "/infra-service/dictItem/save",
  itemEdit = "/infra-service/dictItem/update",
  dictItemCheck = "/infra-service/dictItem/dictItemCheck",
  refreshCache = "/infra-service/dict/refreshCache",

  saveGroup = "/infra-service/dictGroup/save",
  updateGroup = "/infra-service/dictGroup/update",
  batchDeleteGroup = "/infra-service/dictGroup/batchDelete",
  deleteGroup = "/infra-service/dictGroup/delete",
  listDictItem = "/infra-service/dictItem/listItem",
}
/**
 * 导出api
 * @param params
 */
export const getExportUrl = "/infra-service/dict/exportExcel"
/**
 * 导入api
 * @param params
 */
export const getImportUrl = "/infra-service/dict/importExcel"
export const getDict = (params) => defHttp.get({ url: Api.getDict, params }, { joinParamsToUrl: true })

export const listDictItem = (typeCode) => {
  return defHttp.get({
    url: Api.listDictItem,
    params: {
      typeCode: typeCode,
    },
  })
}

export const loadDictItem = (dict: string, key: string) => {
  return defHttp.get({
    url: `/infra-service/dictItem/loadDictItem`,
    params: {
      dict: dict,
      key: key,
    },
  })
}

/** 根据关键字获取字典项（搜索） */
export const loadDictByKeyword = (dict: string, keyword: string, isTransformResponse: boolean = false) => {
  console.log("loadDictByKeyword", dict, keyword)
  return defHttp.get(
    {
      url: `/infra-service/dict/loadDict`,
      params: {
        keyword: keyword,
      },
    },
    {
      isTransformResponse,
    }
  )
}

/**
 * 字典列表接口
 * @param params
 */
export const list = (params) => defHttp.get({ url: Api.list, params })
/**
 * 删除字典
 */
export const deleteDict = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.deleteDict, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess()
  })
}
/**
 * 批量删除字典
 * @param params
 */
export const batchDeleteDict = (params, handleSuccess) => {
  Modal.confirm({
    title: "确认删除",
    content: "是否删除选中数据",
    okText: "确认",
    cancelText: "取消",
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess()
      })
    },
  })
}
/**
 * 保存或者更新字典
 * @param params
 */
export const saveOrUpdateDict = (params, isUpdate) => {
  const url = isUpdate ? Api.edit : Api.save
  return defHttp.post({ url: url, params })
}
/**
 * 唯一校验
 * @param params
 */
export const duplicateCheck = (params) => defHttp.get({ url: Api.duplicateCheck, params }, { isTransformResponse: false })
/**
 * 字典回收站列表
 * @param params
 */
export const getRecycleBinList = (params) => defHttp.get({ url: Api.recycleBinList, params })
/**
 * 回收站还原
 * @param params
 */
export const putRecycleBin = (id, handleSuccess) => {
  return defHttp.put({ url: Api.putRecycleBin + `/${id}` }).then(() => {
    handleSuccess()
  })
}
/**
 * 回收站删除
 * @param params
 */
export const deleteRecycleBin = (id, handleSuccess) => {
  return defHttp.delete({ url: Api.deleteRecycleBin + `/${id}` }).then(() => {
    handleSuccess()
  })
}
/**
 * 字典配置列表
 * @param params
 */
export const itemList = (params) => defHttp.get({ url: Api.itemList, params })
/**
 * 字典配置删除
 * @param params
 */
export const deleteItem = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.deleteItem, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess()
  })
}
/**
 * 保存或者更新字典配置
 * @param params
 */
export const saveOrUpdateDictItem = (params, isUpdate) => {
  const url = isUpdate ? Api.itemEdit : Api.itemSave
  return defHttp.post({ url: url, params })
}

export const dictItemCheck = (params) => defHttp.get({ url: Api.dictItemCheck, params }, { isTransformResponse: false })

export const refreshCache = () => defHttp.get({ url: Api.refreshCache }, { isTransformResponse: false })

export const queryAllDictItems = () => {
  return defHttp.get({ url: "/infra-service/dict/listAllDictItem" }, { isTransformResponse: false })
}


export const saveOrUpdateGroup = (params, isUpdate) => {
  const url = isUpdate ? Api.updateGroup : Api.saveGroup
  return defHttp.post({ url: url, params })
}

export const deleteGroup = (params, handleSuccess) => {
  Modal.confirm({
    title: `确认删除${params.name}`,
    content: "是否删除选中数据",
    okText: "确认",
    cancelText: "取消",
    onOk: () => {
      return defHttp.delete({ url: Api.deleteGroup, data: { id: params.id } }, { joinParamsToUrl: true }).then(() => {
        handleSuccess()
      })
    },
  })
}

export const batchDeleteGroup = (params, handleSuccess) => {
  Modal.confirm({
    title: `确认删除`,
    content: "是否删除选中数据",
    okText: "确认",
    cancelText: "取消",
    onOk: () => {
      return defHttp.delete({ url: Api.batchDeleteGroup, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess()
      })
    },
  })
}
